/**
 * 
 */
package com.thoughtworks.merchantsguide.processor;

import org.apache.commons.lang.StringUtils;

import com.thoughtworks.merchantsguide.cache.ComodityPriceCache;
import com.thoughtworks.merchantsguide.exception.InvalidInputException;

/**
 * This processor, parses the input and creates the grammar for galatic
 * transformation into roman numbers.
 *
 */
public class GalacticNumberGrammarProcessor implements LineProcessor {

	/**
	 * This method parses the string and creates the Commodity Price cache for
	 * acceptable units in galactic language.
	 * 
	 * @throws InvalidInputException
	 * 
	 * @see com.thoughtworks.merchantsguide.processor.LineProcessor#process(java.lang.String)
	 */
	@Override
	public Object process(String query) throws InvalidInputException {
		StringBuilder galacticGrammar = new StringBuilder();
		if (null != query && !query.isEmpty()) {
			String[] stringArr = StringUtils.split(query);
			if (stringArr.length == 3) {
				ComodityPriceCache.addSupportedNumberSystem(stringArr[0], stringArr[2]);
				galacticGrammar.append(stringArr[0]).append(" -> ").append(stringArr[2]);
			} else {
				throw new InvalidInputException("Galactic to Roman number information is invalid.");
			}
		}
		return galacticGrammar.toString();
	}
}